package com.cksys.achievement.controller.viewcontroller;

import com.cksys.achievement.entity.User;
import com.cksys.achievement.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ClassName: IndexViewController
 * @author: zyx
 * @E-mail: 1377631190@qq.com
 * @DATE: 2019/9/23 10:23
 */
@CrossOrigin
@Controller
public class IndexViewController {

    @Value("${qq.redirect}")
    String qqRedirectUrl;

    @Value("${qq.client.id}")
    String qqClientId;

    @Autowired
    IUserService userService;

    @GetMapping({"/", "/index"})
    public String index() {
        return "index/index";
    }

    @RequestMapping({"/showresult", "/showresult/index"})
    public String showResultIndexChoose() {
        return "showresult/index";
    }

    @RequestMapping({"/manage", "/manage/index"})
    public String showManage() {
        return "manage/index";
    }

    @RequestMapping("/person")
    public String showPerson(@AuthenticationPrincipal UserDetails userDetails,
                             HttpServletRequest request) {
        String username = userDetails.getUsername();
        if (null != username) {
            User user = userService.getByUserName(username);
            request.setAttribute("user", user);
        }
        return "person/index";
    }

    @GetMapping("/myAdd")
    public String showMyAdd() {
        return "person/myadd";
    }

    @RequestMapping("/test")
    public String test() {
        return "test/demo";
    }

    @GetMapping("/binding/qq")
    public void bindingQQ(HttpServletResponse response) throws IOException {
        String url = "https://graph.qq.com/oauth2.0/authorize?which=Login&response_type=code&client_id=%s&redirect_uri=%s&state=%s";
        url = String.format(url, qqClientId, qqRedirectUrl, (Math.random()*9+1)*100000);
        response.sendRedirect(url);
    }

    @GetMapping("/binding")
    public String binding() {
        return "binding/binding";
    }

    @GetMapping("/binding/register")
    public String bindingRegister() {
        return "binding/register";
    }

    @GetMapping("/forget")
    public String forget() {
        return "utils/forget";
    }

    /*-------------后台开始-------------*/

    @GetMapping("/admin")
    public String adminLogo() {
        return "admin/login";
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @GetMapping("/admin/index")
    public String adminIndex(@AuthenticationPrincipal UserDetails userDetails,
                             HttpServletRequest request) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        System.out.println(SecurityContextHolder.getContext());
        String username = userDetails.getUsername();
        if (null != username) {
            User user = userService.getByUserName(username);
            request.getSession().setAttribute("user", user);
        }
        return "admin/index";
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @GetMapping("/admin/result")
    public String adminIndex() {
        return "admin/results";
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @GetMapping("/admin/users")
    public String adminUsersIndex() {
        return "admin/users";
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @GetMapping("/admin/person")
    public String adminPersonIndex() {
        return "admin/person";
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @GetMapping("/admin/type")
    public String adminTypeIndex() {
        return "admin/type";
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @GetMapping("/admin/table")
    public String adminTableIndex() {
        return "admin/table";
    }
}
